#!/usr/bin/env python
# encoding: utf-8


"""
@file: sanjiao_duichengxing.py
@time: 2017/5/16 上午11:09
"""
# 三角函数对称性
from mathsolver.functions.base import *
from mathsolver.functions.budengshi import common_opers as co
from mathsolver.functions.sanjiao import sanjiao_utils as su
from sympy import sin, cos, Abs, tan


# 三角函数对称性求参-系数含参
# Input paramer1:三角函数; paramer2:对称中心
class SanJiaoHanShuDuiChengXingQiuCan(BaseFunction):
    def solver(self, *args):
        self.label.add('三角函数对称性求参-系数含参')
        arg1, arg2 = args
        arg1 = list(co.flatten([arg1.sympify()]))
        if len(arg1) == 1:
            f_symbol = 'f(x)'
            f_expr = arg1[0]
        elif len(arg1) == 2:
            f_symbol, f_expr = arg1
        else:
            raise Exception('Illegal parameter')
        _x = sympify('x')
        center_x, center_y = arg2.sympify()
        sin_p = r'sin\(.*?\)'
        sin_terms = co.find_reg_expr(sin_p, f_expr)
        cos_p = r'cos\(.*?\)'
        cos_terms = co.find_reg_expr(cos_p, f_expr)
        if not (sin_terms or cos_terms):
            raise Exception('Can solve the type question')
        self.steps.append(['由' + BaseEq([f_symbol, f_expr]).printing(),
                           '一个对称中心为:' + '(%s, %s)' % (new_latex(center_x), new_latex(center_y))])
        f1 = f_expr.subs(_x, center_x)
        self.steps.append(['可得', BaseEq([f1, center_y]).printing()])
        result_eq = BaseEq([f1, center_y])
        self.output.append(result_eq)
        return self


# 三角函数对称轴/对称中心 不落在某个区间
# Input paramer1:三角函数; paramer2:区间; paramer3:对称轴/对称中心
class SanJiaoHanShuDuiChengYuQuJian(BaseFunction):
    def solver(self, *args):
        self.label.add('函数对称轴不在某范围内，求参数w取值范围')
        arg1 = list(co.flatten([args[0].sympify()]))
        if len(arg1) == 1:
            f_symbol = 'y'
            f_expr = arg1[0]
        elif len(arg1) == 2:
            f_symbol, f_expr = arg1
        else:
            raise Exception('Illegal parameter')
        arg2 = args[1]
        if isinstance(arg2, BaseInnerSet):
            intl = arg2.value['value']
        elif isinstance(arg2, BaseInter):
            intl = arg2.interval
        else:
            raise Exception('Illegal parameter')
        left = intl.left
        right = intl.right
        left_open = intl.left_open
        right_open = intl.right_open
        text = args[2]
        f_simp = su.i_trig_simplify(f_expr)
        _x = sympify('x')
        _k = sympify('k')
        self.steps.append(['由题可知:', BaseEq([f_symbol, f_expr]).printing() + '=' + new_latex(f_simp)])
        t_coef, t_type, t_arg, t_const = su.simp_trig_info(f_simp)
        if t_type in [sin, cos]:
            t_coef, t_type, t_arg, t_const = su.cos_2_sin(f_simp)
            _w = t_arg.coeff(_x)
            _h = t_arg.subs(_x, 0)
            op1 = '>=' if left_open and right_open else '>'
            op2 = '<=' if left_open else '<'
            op3 = '>=' if right_open else '>'
            if text.find('对称轴') >= 0:
                ineq1 = [pi / _w, op1, Abs(left - right)]
                ineq2 = [(_k * pi + pi / 2 - _h) / _w, op2, left]
                ineq3 = [((_k + 1) * pi + pi / 2 - _h) / _w, op3, right]
            else:  # 对称中心
                ineq1 = [pi / _w, op1, Abs(left - right)]
                ineq2 = [(_k * pi - _h) / _w, op2, left]
                ineq3 = [((_k + 1) * pi - _h) / _w, op3, right]
            ineqs = [ineq1, ineq2, ineq3]
        elif t_type == tan:  # tan
            op1 = '>=' if left_open and right_open else '>'
            op2 = '<=' if left_open else '<'
            op3 = '>=' if right_open else '>'
            _w = t_arg.coeff(_x)
            _h = t_arg.subs(_x, 0)
            ineq1 = [pi / (2 * _w), op1, Abs(left - right)]
            ineq2 = [(_k * pi / 2 - _h) / _w, op2, left]
            ineq3 = [((_k + 1) * pi / 2 - _h) / _w, op3, right]
            ineqs = [ineq1, ineq2, ineq3]
        else:
            raise Exception('Can not solve the question')
        self.steps.append(['得到不等式组', BaseIneqs(ineqs).printing() + '其中' + su.print_belong_z('k')])
        self.output.append(BaseIneqs(ineqs))
        self.output.append(BaseSymbolValue({_k: S.Integers}))
        return self


if __name__ == '__main__':
    pass
